package jnpf.property.controller;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.property.entity.ZcInspectionPlanEntity;
import jnpf.property.entity.ZcInspectionPlanVo;
import jnpf.property.entity.ZcInspectionTaskEntity;
import jnpf.property.model.inspection.*;
import jnpf.property.service.ZcInspectionPlanService;
import jnpf.property.service.ZcInspectionTaskService;
import jnpf.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 巡检管理
 */
@Slf4j
@RestController
@Api(tags = "ZcInspectionPlan", value = "巡检管理")
@RequestMapping("/api/property/inspection")
public class ZcInspectionController {

    @Autowired
    private ZcInspectionPlanService planService;

    @Autowired
    private ZcInspectionTaskService taskService;

    /**
     * 创建巡检计划
     *
     * @param form 巡检计划表单
     */
    @PostMapping("/plan/create")
    @ApiOperation("创建计划")
    public ActionResult<?> create(@RequestBody @Validated ZcInspectionPlanForm form) {
        if (planService.creatPlan(form)) {
            return ActionResult.success("创建成功");
        } else {
            return ActionResult.fail("创建失败");
        }
    }

    /**
     * 信息
     *
     * @param id
     * @return
     */
    @ApiOperation("计划信息")
    @GetMapping("/plan/{id}")
    public ActionResult<ZcInspectionPlanVo> info(@PathVariable("id") String id) {
        return ActionResult.success(planService.getPlanInfo(id));
    }

    /**
     * 更新
     *
     * @param form 巡检计划表单
     * @return
     */
    @PutMapping("/plan/update")
    @ApiOperation("更新")
    public ActionResult<?> update(@RequestBody ZcInspectionPlanForm form) {
        if (StrUtil.isEmpty(form.getId())) {
            return ActionResult.fail("更新计划ID为空");
        }
        if (planService.updatePlan(form)) {
            return ActionResult.success("更新成功");
        } else {
            return ActionResult.fail("更新失败");
        }

    }


    /**
     * 删除
     *
     * @param ids 删除计划ID
     */
    @ApiOperation("删除")
    @DeleteMapping("/plan/{ids}")
    public ActionResult<?> delete(@PathVariable("ids") String[] ids) {
        if (planService.removeBatchByIds(Arrays.asList(ids))) {
            return ActionResult.success("删除成功");
        } else {
            return ActionResult.fail("删除失败");
        }

    }


    /**
     * 计划列表
     *
     * @param pagination 查询条件
     * @return
     */
    @ApiOperation("获取列表")
    @PostMapping("/plan/getList")
    public ActionResult<?> list(@RequestBody ZcInspectionPlanPagination pagination) {
        List<ZcInspectionPlanEntity> list = planService.getPlanList(pagination);

        PageListVO<ZcInspectionPlanEntity> vo = new PageListVO<>();
        vo.setList(list);
        PaginationVO page = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);
    }

    /**
     * 任务信息
     *
     * @param taskId
     * @return
     */
    @ApiOperation("计划信息")
    @GetMapping("/task/{id}")
    public ActionResult<ZcInspectionTaskDetail> getTaskInfo(@PathVariable("id") String taskId) {
        return ActionResult.success(taskService.getTaskInfo(taskId));
    }

    @ApiOperation("获取巡检任务列表")
    @PostMapping("/task/getList")
    public ActionResult<?> taskList(@RequestBody ZcInspectionTaskPagination pagination) {
        List<ZcInspectionTaskEntity> taskList = taskService.getTaskList(pagination);

        PageListVO<ZcInspectionTaskEntity> vo = new PageListVO<>();
        vo.setList(taskList);
        PaginationVO page = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);
    }

    @ApiOperation("巡检任务执行")
    @PostMapping("/task/execute")
    public ActionResult<?> executeTaskItem(@RequestBody @Validated ZcInspectionTaskItemForm form){
        taskService.executeTaskItem(form);
        return ActionResult.success("操作成功!");
    }

}
